﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ExeInvoker
{
    /// <summary>
    /// ProgramAddOrEdit.xaml 的交互逻辑
    /// </summary>
    public partial class ProgramAddOrEdit : Window
    {
        internal ProgramInfoViewModel ProgramInfo { get; set; }

        private ProgramInfoViewModel oldProgramInfo = null;

        public ProgramAddOrEdit()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.ProgramInfo != null)
            {
                this.oldProgramInfo = this.ProgramInfo;
                this.ProgramInfo = this.ProgramInfo.Clone() as ProgramInfoViewModel;
            }
            else
            {
                this.ProgramInfo = new ProgramInfoViewModel();
            }
            this.tbProgramArgs.DataContext = this.ProgramInfo;
            this.tbProgramPath.DataContext = this.ProgramInfo;
            this.tbStrokeKeys.DataContext = this.ProgramInfo;
        }

        private void btnChoose_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            if (ofd.ShowDialog().Value == false)
            {
                return;
            }
            this.ProgramInfo.ProgramPath = ofd.FileName;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(this.ProgramInfo.ProgramPath) && string.IsNullOrWhiteSpace(this.ProgramInfo.ProgramArgs))
                {
                    MessageBox.Show("请输入相应的程序与参数");
                    return;
                }

                if (this.oldProgramInfo != null)
                {
                    ProgramInfoViewModelManager.Instance.Remove(this.oldProgramInfo);
                }
                ProgramInfoViewModelManager.Instance.Add(this.ProgramInfo);
                ProgramInfoViewModelManager.Instance.Save();
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
